package com.jstyle.jclife.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jstyle.jclife.R;
import com.jstyle.jclife.activity.HealthHistoryActivity;
import com.jstyle.jclife.model.HealthData;
import com.jstyle.jclife.utils.DateUtil;
import com.jstyle.jclife.utils.ResolveData;

import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/25.
 */

public class HrvDataAdapter extends RecyclerViewBaseAdapter {

    int dataType= HealthHistoryActivity.DATA_HRV;
    public HrvDataAdapter(List<HealthData> healthData) {
        super(healthData);
    }
    public void setDataType(int dataType){
        this.dataType=dataType;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
        return new HrvViewHolder(view);
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        HealthData healthData= (HealthData) mDataList.get(position);
        HrvViewHolder hrvViewHolder=(HrvViewHolder) holder;
        String[] times=healthData.getTime().split(" ");
        String healthValue="";
        String levelResult="";
        Context context=hrvViewHolder.itemView.getContext();
        if(dataType==HealthHistoryActivity.DATA_PRESSURE){
            healthValue= String.valueOf(healthData.getPressure());
            int level=ResolveData.getStressLevel(healthData.getPressure());
            levelResult=hrvViewHolder.arrayStressString[level];
        }else if(dataType==HealthHistoryActivity.DATA_HRV){
            int level=ResolveData.getHrvLevel(healthData.getHrv());
            healthValue= String.valueOf(healthData.getHrv());
            levelResult=hrvViewHolder.arrayHrvString[level];
        }else if(dataType==HealthHistoryActivity.DATA_BLOOD){
            int high=healthData.getHighBloodPressure();
            int low=healthData.getLowBloodPressure();
            healthValue=String.valueOf(high)+"/"+String.valueOf(low);
            levelResult=hrvViewHolder.itemView.getContext().getString(R.string.bp_day_level);
        }
        hrvViewHolder.tvHrvValue.setText(healthValue);
        hrvViewHolder.tvHrvLevel.setText(levelResult);
        hrvViewHolder.tvHrvdataDate.setText(DateUtil.getShowDay(context,times[0]));
        hrvViewHolder.tvHrvdataTime.setText(times[1].substring(0,5));
    }

    public class HrvViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_hrvdata_date)
        TextView tvHrvdataDate;
        @BindView(R.id.tv_hrvdata_time)
        TextView tvHrvdataTime;
        @BindView(R.id.tv_hrv_value)
        TextView tvHrvValue;
        @BindView(R.id.tv_hrv_level)
        TextView tvHrvLevel;
        @BindArray(R.array.string_pressure_array)
        String[] arrayStressString;
        @BindArray(R.array.string_bp_array)
        String[] arrayBp;
        @BindArray(R.array.string_hrv_array)
        String[] arrayHrvString;
        
        public HrvViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    protected int getLayoutId(int viewType) {
        return R.layout.item_hrvdata_day;
    }
}
